// 计算表格需要使用的数据
import { useAnalysisLog, analysisItem } from '../../hook/analysisLog';
import { barChartTemplate, pieChartTemplate, geoChartTemplate, geoCoordMap } from './template';

export function getAppEchartsConfig(appData: analysisItem) {
    const seriesData = [];

    const xAxisData = [];

    Object.keys(appData).map((name) => {
        const item = appData[name];
        Object.keys(item.subItem).map((version) => {
            const versionItem = item.subItem[version];

            xAxisData.push(name + version);

            seriesData.push(versionItem.count);
        });
    });

    return barChartTemplate('APP 分布', seriesData, xAxisData);
};

export function getPlatformEchartsConfig(platForm: analysisItem) {
    const seriesData = [];
    const xAxisData = [];

    Object.keys(platForm).map((name) => {
        const item = platForm[name];
        Object.keys(item.subItem).map((version) => {
            const versionItem = item.subItem[version];
            xAxisData.push(name + version);
            seriesData.push(versionItem.count);
        });
    });

    return barChartTemplate('Platform 分布', seriesData, xAxisData);
};

export function getWebviewEchartsConfig(webviewData: analysisItem) {
    const seriesData = [];

    const xAxisData = [];
    Object.keys(webviewData).map((name) => {
        const item = webviewData[name];
        xAxisData.push(name);
        seriesData.push(item.count);
    });

    return barChartTemplate('WebView 分布', seriesData, xAxisData);
}

export function getISPEchartsConfig(ISPData: analysisItem) {
    const seriesData = [];

    Object.keys(ISPData).map((name) => {
        const item = ISPData[name];

        if (item.count === 0) {
            return;
        }

        seriesData.push({
            value: item.count,
            name
        });
    });
    return pieChartTemplate('运营商分布', seriesData);
}

export function getFromEchartsConfig(fromData: analysisItem) {
    const seriesData = [];

    const xAxisData = [];
    Object.keys(fromData).map((name) => {
        const item = fromData[name];
        xAxisData.push(name);
        seriesData.push(item.count);
    });

    return barChartTemplate('From 分布', seriesData, xAxisData);
}

export function getNetTypeEchartsConfig(netTypeData: analysisItem) {
    const seriesData = [];

    Object.keys(netTypeData).map((name) => {
        const item = netTypeData[name];

        if (item.count === 0) {
            return;
        }

        seriesData.push({
            value: item.count,
            name
        });
    });
    return pieChartTemplate('NetType分布', seriesData);
}

export function getVersionEchartsConfig(versionData: analysisItem) {
    const seriesData = [];

    Object.keys(versionData).map((name) => {
        const item = versionData[name];

        if (item.count === 0) {
            return;
        }

        seriesData.push({
            value: item.count,
            name
        });
    });
    return pieChartTemplate('Version分布', seriesData);
}

export function getMapEchatsConfig(mapData: analysisItem) {
    const formatData = [];
    let maxCount = 0
    for (let key in mapData) {
        if (geoCoordMap[key]) {
            mapData[key].count > maxCount ? maxCount = mapData[key].count : null;
            formatData.push({
                name: key,
                value: geoCoordMap[key].concat(mapData[key].count)
            });
        } else {
            console.warn('no city location data:', key);
        }
    }
    return geoChartTemplate('分地区统计', formatData, maxCount);
}

